package User;

public class UserDao {
}
package com.sns.dao;

import com.sns.model.User;
import com.sns.util.DatabaseUtil;
import java.sql.*;
        import java.util.ArrayList;
import java.util.List;

public class UserDao {
    public boolean addUser(User user) {
        String sql = "INSERT INTO User (USERID, PASSWORD, NICKNAME, AVATAR, PHONE, EMAIL, BIRTHDATE, GENDER) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, user.getUserId());
            stmt.setString(2, user.getPassword());
            stmt.setString(3, user.getNickname());
            stmt.setString(4, user.getAvatar());
            stmt.setString(5, user.getPhone());
            stmt.setString(6, user.getEmail());
            stmt.setDate(7, new java.sql.Date(user.getBirthdate().getTime()));
            stmt.setString(8, user.getGender());

            return stmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public User getUserById(String userId) {
        String sql = "SELECT * FROM User WHERE USERID = ?";
        User user = null;

        try (Connection conn = DatabaseUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, userId);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                user = new User();
                user.setUserId(rs.getString("USERID"));
                user.setNickname(rs.getString("NICKNAME"));
                user.setAvatar(rs.getString("AVATAR"));
                // 设置其他字段...
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return user;
    }

    // 其他方法...
}